<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template>
          <span>总记录数:  1000条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button type="danger" size="small" @click="hExportExcel">excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <!-- <el-table-column label="头像" />
          <template v-slot="scope"> -->
          <!-- <img :src="scope.row.staffPhoto"> -->
          <!-- <ImageHolder :src="scope.row.staffPhoto" /> -->
          <template />
          <el-table-column />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="scope">
              类型：{{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" />
          <el-input
            style="width:50%"
            placeholder="请选择部门"
          />
          <div v-if="showTree" class="tree-box">
            <el-tree :data="treeData" :props="{ label: 'name' }" />
          </div>
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />

          <el-table-column label="操作" width="280">
            <template v-slot="scope">
              <el-button type="primary" size="small" @click="$router.push('/employees/detail?id=' + scope.row.id)">查看</el-button>
              <el-button type="primary" size="small">分配角色</el-button>

              <el-button type="primary" size="small" @click="doDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
          <!-- </el-table></el-card></div></div></template></el-table> -->
          <!-- 分页组件 -->
          <el-row type="flex" justify="center" align="middle" style="height: 60px">
            <div style="height: 60px; margin-top: 10px">
              <!-- 分页 -->
              <el-pagination
                layout="total, sizes,prev, pager, next, jumper"
                :total="total"
                :page-size="size"
                :current-page="page"
                :page-sizes="[2,5,10]"
                @current-change="hCurrentChange"
                @size-change="hSizeChange"
              />
            </div>
          </el-row>
        </el-table></el-card>
    </div>
    <el-dialog title="新增员工" :visible.sync="showDialog">
      <addor-edit />
    </el-dialog>
  </div>
</template>
<script>
// import hAssignRole from './hAssignRole.vue'
import { getEmployeess, deleteRole } from '@/api/employees.js'
import employees from '@/constant/employees.js'
import addorEdit from './empDialog.vue'
const hireType = employees.hireType.reduce((acc, item) => { return { ...acc, [item.id]: item.value } }, {})

export default {
  components: { addorEdit },
  hEditRole({ id }) {
    console.log('当前要分配角色id是', id)
    this.showRoleDialog = true
  },
  data() {
    return {
      showTree: false,
      list: [],
      page: 1, // 当前页码
      size: 5, // 每页几条
      total: 0, // 总共数据条数
      showDialog: false // 弹框

    }
  },
  created() {
    this.loadEmployeess()
  },
  methods: {

    // hExportExcel() {
    //   // 导入数据为excel文件
    //   import ('@/vendor/Export2Excel').then(excel => {
    // //     console.log('导出数据为excel文件', excel)
    //   })
    // },
    transformData(rows) {
    // rows:  [
    //   {id:'01', mobile:'13612345678'},
    //   {id:'02', mobile:'13612345679'}
    // ]
      const header = Object.keys(rows[0]) // ['id', 'mobile']
      const data = rows.map(item => {
        return header.map(key => {
          return item[key]
        })
      // return Object.values(item)
      })
      // todo: (1)表头要是中文的 (2) 聘用形式 不要是1，而是'正式'

      // [
      //   ['01', '13612345678'],
      //   ['02', '13612345679']
      // ]
      return {
        header, data
      }
    },
    hExportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
      // excel表示导入的模块对象
      // const res = await hib({page:this.page,size:this.size})
      // 1. 发请求
        const res = await getEmployeess({ page: this.page, size: this.size })
        console.log('要导出的数据', res.data.rows)
        console.log('我要导出数据', res.data.rows)
        const { header, data } = this.transformData(res.data.rows)

        console.log(excel)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: '员工信息表', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    async loadEmployeess() {
      const res = await getEmployeess({ page: this.page, size: this.size })
      // console.log(res)
      console.log('获取数据11111111', res)
      this.total = res.data.total
      this.list = res.data.rows
    },
    formatEmployment(id) {
      return hireType[id]
    },
    // 删除
    async doDel(id) {
      try {
        const res = await deleteRole(id)
        console.log(res)
        // 提示
        this.$message.success('删除成功')
        // 如果当前表格中只有一条，删除之后自动请求上一页数据
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        // 重新请求
        this.loadEmployeess()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },

    hCurrentChange(curPage) {
      console.log(curPage)
      // alert(curPage)
      // 1. 更新页码
      this.page = curPage
      // 2. 重发请求
      this.loadEmployeess()
    },
    // 每页几条
    hSizeChange(curSize) {
    // alert(size)
    // 1. 更新每页的条数
      this.size = curSize
      // 2. 重发请求
      this.loadEmployeess()
    }
  }

}

</script>
